// 導入
import { ElementRef, Injectable, ViewChild } from '@angular/core';
import { Component,OnInit, AfterViewInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ImportService }  from './import.service';

import { HttpClient } from "@angular/common/http";

@Injectable({
  providedIn: 'root',
})

@Component({
  selector: 'import',
  templateUrl: './import.component.html',
  styleUrls: ['./import.component.css']
})

export class ImportComponent implements OnInit, AfterViewInit, OnDestroy {
  uploadTitleForItem:string = "画面項目記述書";
  uploadTitleForClassList:string = "クラス一覧";
  uploadProcessList:string = "処理一覧";
  inputdataForItem = [];//画面項目記述書
  inputdataForItemFile = [];
  inputdataForClassList = [];//クラス一覧
  inputdataForProcessList = [];//处理一覧
  uploadTitleForLogicalDataItem:string = "論理データ項目一覧";
  inputdateForLogicalDataItem = [];//論理データ項目一覧
  uplaodType:string="0"

  //ヘーダ
  mixHeaderArrForItem = ["section","levelCd","repeatedReturn","screenItemName","screenItemPhysicsName",
  "ioCd","typeCd","attributeCd","tabCd","required","rangeWide","rangeSmall",
  "digitBranch","numberBar","special","regularExpression","displayFormatCd",
  "controlCondition","dataItem","processingId","supplements"];
  mixHeaderArrForClassItem = ["seqId",
  "processId",
  "componentType",
  "subPackage",
  "classLogicName",
  "classPhysicsName",
  "comment",
  "typeKubun",
  "type",
  "typeKubunOther",
  "typeOtherSub",
  "saveProject",
  "team"];
  mixHeaderArrForProcessItem = ["no",
                                "processId",
                                "processName",
                                "processAbstract",
                                "kubun",
                                "screenId",
                                "invoiceId",
                                "sysInterfaceId",
                                "collectProcessId",
                                "sysFunctionId",
                                "bizPatternId",
                                "pageProcessPattern",
                                "aynProcessPattern",
                                "transection",
                                "dataStopFlag",
                                "typeKubun",
                                "type",
                                "comment",
                                "",
                                "closeFlag",
                                "transToPage",
                                "batchType",
                                "ajaxKubun"];
  mixHeaderArrForLgcDtaItem = ["consecutive_no","entity_physical_name","entity_logical_name","data_item_name",
  "column_cd","pk","fy","related_entity_name","data_type_cd","size","explanation_items"];

  @ViewChild('uploadButton',{static:true}) uploadButtonRef : ElementRef;

  ngOnInit(): void {}
  ngAfterViewInit(): void {
    this.setDisabled();
  }
  ngOnDestroy(): void {}

  constructor(
    private http:HttpClient,
    private route: ActivatedRoute,
    private service: ImportService,
  ) {}

  postFile(files: FileList){
    let flag:boolean = false;
    const formData = new FormData();
    formData.append( 'file', files[0]);
    this.http.post("saveExcelDataToDB1", formData).subscribe(data => {
      flag = true;
      return;
  })
 }
 
  // 导入
  upload(evt: any) {
    let flag:boolean = false;
    
    //画面項目記述書
    if(this.inputdataForItemFile.length != 0){

      const formData = new FormData();
      formData.append('file', this.inputdataForItemFile[0]);
      this.http.post("saveExcelDataToDB1", formData).subscribe(data => {
        flag = true;
        alert("アップロード完了しました");
        return;
    })

      // this.service.saveExcelToDB(this.inputdataForItem).subscribe(
      //   data => {
      //       flag = true;
      //       return;
      //   }
      // );
    }
    //クラス一覧
    if(this.inputdataForClassList.length != 0){
      this.service.saveExcelToDbForClassList(this.inputdataForClassList,this.uplaodType).subscribe(
        data => {
          alert("アップロード完了しました");
            return;
        }
      );
    }
    //論理データ項目一覧
    if(this.inputdateForLogicalDataItem.length != 0){
      this.service.saveExcelToDbForLgcDtaItem(this.inputdateForLogicalDataItem).subscribe(
        data => {
          alert("アップロード完了しました");
            return;
        }
      );
    }
    //論理データ項目一覧
    if(this.inputdataForProcessList.length != 0){
      this.service.saveExcelToDbForProcessList(this.inputdataForProcessList).subscribe(
        data => {
          alert("アップロード完了しました");
            return;
        }
      );
    }
  }
  //画面項目記述書
  setDataForItem(inputdata:any[]){
    this.inputdataForItemFile = inputdata;
    this.setDisabled();
  }
  //クラス一覧
  setDataForClassList(inputdata:any[]){
    this.inputdataForClassList = inputdata;
    this.setDisabled();
  }
  //処理一覧
  setDataForProcessList(inputdata:any[]){
    this.inputdataForProcessList = inputdata;
    this.setDisabled();
  }
  //論理データ項目一覧
  setDataForLgcDtaItem(inputdata:any[]){
    this.inputdateForLogicalDataItem = inputdata;
    this.setDisabled();
  }

  //アップロードボタンをコントロール
  setDisabled(){
    debugger
    let itemFlag:boolean = this.inputdataForItem.length != 0;
    let classListFlag:boolean = this.inputdataForClassList.length != 0;
    let processListFlag:boolean = this.inputdataForProcessList.length != 0;
    let lgcDtaItemFlag:boolean = this.inputdateForLogicalDataItem.length != 0;
    if(itemFlag && classListFlag && processListFlag && lgcDtaItemFlag){
    
    if(itemFlag && classListFlag){
      this.uplaodType = "99"
    }else if(itemFlag){
      this.uplaodType = "01"
    }else if(classListFlag){
      this.uplaodType = "02"
    }else if(lgcDtaItemFlag){
      this.uplaodType = "03"
    }else if(processListFlag){
      this.uplaodType = "04"
    }else{
      this.uplaodType = "00"
    }
    let flag = itemFlag || classListFlag || lgcDtaItemFlag || processListFlag;
    this.uploadButtonRef.nativeElement.disabled = !flag;
  }
  }
}
